package virtorina.client.rooms;

import java.util.ArrayList;
import java.util.BitSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.LinkedList;

import virtorina.client.admin.dto.LabelDTO;
import virtorina.client.room.rpc.GWTRoomsService.Util;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.SimpleCheckBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class addRoom {
	class MyLabel extends HTML {
		int rank;
		boolean active;
		Long id;

		MyLabel(String code, int rank, Long id) {
			super(code);
			this.rank = rank;
			active = false;
			this.id = id;
			super.addClickHandler(new ClickHandler() {

				@Override
				public void onClick(ClickEvent event) {
					if (active) {
						active = false;
						RootPanel.get("active").remove(addRoom.MyLabel.this);
						returnToPanel(addRoom.MyLabel.this);
					} else {
						active = true;
						RootPanel.get("active").add(addRoom.MyLabel.this);
					}

				}
			});

		}
	}

	private void returnToPanel(MyLabel ml) {
		MyLabel t;
		int q = 0;
		for (Iterator<Widget> i = vp.iterator(); i.hasNext(); q++) {
			t = (MyLabel) i.next();
			if (t.rank > ml.rank) {
				vp.insert(ml, q);
				vp.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
				return;
			}
		}
	}

	private void makeRoom() {
		LinkedHashSet<Long> labels = new LinkedHashSet<Long>();
		Long t;
		String name = this.name.getText();
		if (name.length() < 3) {
			setMessage("The room name is too short");
			return;
		} else if (name.length() > 30) {
			setMessage("The room name is too long");
			return;
		}
		LinkedList<Integer> types = new LinkedList<Integer>();
		if (t1.getValue()){
			types.add(0);
		}
		if (t2.getValue()){
			types.add(1);
		}
		if (t3.getValue()){
			types.add(2);
		}
		if (t4.getValue()){
			types.add(3);
		}
		if (types.isEmpty()){
			setMessage("Add question types");
			return;
		}
		int q = 0;
		for (Iterator<Widget> i = RootPanel.get("active").iterator(); i
				.hasNext();q++) {
			t = ((MyLabel) i.next()).id;
			if (t.equals(0L)) {
				q++;
				labels.clear();
				break;
			} else {
				labels.add(t);
			}
		}
		if (q==0){
			setMessage("Add subjects");
			return;
		}
		 Util.getInstance().makeRoom(labels, name, types, new AsyncCallback<Integer>() {
			
			@Override
			public void onSuccess(Integer result) {
				if (result >= 0){
					setMessage("success","The room has successfully been create! ");
					mainClass.tmp();
				}else if (result.equals(-1)){
					setMessage("error","Такое имя уже существует");
				}else if (result.equals(-2)){
					setMessage("error","Слишком мало вопросов по выбранным настройкам");
				}else{
					setMessage("error","An error has occurred");
				}
				
			}
			
			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}
		});
	}

	private HTML h = HTML.wrap(DOM.getElementById("active"));
	private VerticalPanel vp = new VerticalPanel();
	private TextBox name = TextBox.wrap(DOM.getElementById("name"));
	private Button send = Button.wrap(DOM.getElementById("create"));
	private SimpleCheckBox t1 = SimpleCheckBox.wrap(DOM.getElementById("t1"));
	private SimpleCheckBox t2 = SimpleCheckBox.wrap(DOM.getElementById("t2"));
	private SimpleCheckBox t3 = SimpleCheckBox.wrap(DOM.getElementById("t3"));
	private SimpleCheckBox t4 = SimpleCheckBox.wrap(DOM.getElementById("t4"));
	private Rooms mainClass;

	public addRoom(Rooms main) {
		mainClass = main;
		t1.setValue(true);
		t2.setValue(true);
		t3.setEnabled(false);
		t4.setValue(true);
		//t3.setValue(true);
		//t4.setValue(true);
		RootPanel.get("allLabels").add(vp);
		vp.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		send.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				makeRoom();

			}
		});
		Util.getInstance().getAllLabels(
				new AsyncCallback<LinkedList<LabelDTO>>() {

					@Override
					public void onSuccess(LinkedList<LabelDTO> result) {
						vp.add(new MyLabel(
								"<span class=\"label label-success\">All subjects</span>",
								0, 0L));
						int i = 1;
						for (LabelDTO ld : result) {
							vp.add(new MyLabel("<span class=\"label\">"
									+ ld.getName() + "</span>", i, ld.getID()));
							i++;
						}

					}

					@Override
					public void onFailure(Throwable caught) {
						// TODO Auto-generated method stub

					}
				});
	}

	void setMessage(String message) {
		setMessage("warning", message);
	}

	void setMessage(String type, String message) {
		if (type.equals("warning")) {
			type = "";
		} else {
			type = "alert-" + type;
		}
		RootPanel.get("message").clear();
		RootPanel.get("message").add(
				new HTML("<div class=\"alert " + type
						+ "\"><a class=\"close\" data-dismiss=\"alert\">×</a>"
						+ message + "</div>"));
	}
}
